package com.loong.nnm.impl;

import com.loong.nnm.entity.devm.NtpParameter;
import com.loong.nnm.services.DevmServices;
import com.loong.nnm.utils.NetconfProtocolUtils;
import com.loong.nnm.utils.XmlBuilderUtils;
import net.juniper.netconf.XML;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("DevmServices")
public class DevmImpl implements DevmServices {


    private static final String PATH_NAME = "xmlns";
    private static final String PATH_VALUE = "urn:ccsa:yang:acc-devm";

//    @Resource
    private XmlBuilderUtils xmlBuilderUtils;
//    @Resource
    private NetconfToolImpl netconfTool = new NetconfToolImpl();

    private String get(String node, String module) {
        //组装报文，需要查询的子树进行过滤
        XML content = xmlBuilderUtils.getXMlCoat();
        XML me = content.append(module);
        me.setAttribute(PATH_NAME, PATH_VALUE);
        System.out.println(content);
        //下发请求，并返回报文
        return netconfTool.get(node, content);
    }

    @Override
    public String getMe(String node) {
        return get(node, "me");
    }

    @Override
    public String configMe(String node) {
        return null;
    }

    @Override
    public String getNtpServers(String node) {
        //组装报文，需要查询的子树进行过滤
        XML content = xmlBuilderUtils.getXMlCoat();
        XML me = content.append("ntp-servers");
        me.setAttribute(PATH_NAME, PATH_VALUE);
        System.out.println(content);
        //下发请求，并返回报文
        return netconfTool.get(node, content);
    }

    @Override
    public String configNtpServers(String node, NtpParameter ntpParameter) {
        StringBuilder context = new StringBuilder();

        context.append("<name>").append(ntpParameter.getName()).append("</name>\n");
        context.append("<ip-address>").append(ntpParameter.getHostIp()).append("</ip-address>\n");
        context.append("<port>").append(ntpParameter.getPort()).append("</port>\n");

        if (!ntpParameter.getNtpVersion().equals(""))
            context.append("<ntp-version>").append(ntpParameter.getNtpVersion()).append("</ntp-version>\n");

        return netconfTool.get(node, " <edit-config>\n" +
                "<target>\n" +
                "   <running/>\n" +
                "</target>\n" +
                "<config>\n" +
                "   <me xmlns=\"urn:ccsa:yang:acc-devm\">\n" +
                "       <ntp-servers>\n" +
                "           <ntp-server>\n" +
                context.toString() +
                "          </ntp-server>\n" +
                "        </ntp-servers>\n" +
                "   </me>\n" +
                "</config>\n" +
                "</edit-config>");
    }

    @Override
    public String getEqs(String node) {
        return get(node, "eqs");
    }

    @Override
    public String getEq(String node, String name) {
        return netconfTool.get(node, "<get>\n" +
                "<filter type=\"subtree\">\n" +

                "<eqs xmlns=\"urn:ccsa:yang:acc-devm\">\n" +

                "</eqs>\n" +

                "</filter>\n" +
                "</get>");
    }

    @Override
    public String getPtps(String node) {
        return get(node, "ptps");
    }

    @Override
    public String getPtp(String node, String name) {
        return null;
    }

    @Override
    public String getCtps(String node) {
        //组装报文，需要查询的子树进行过滤
        XML content = xmlBuilderUtils.getXMlCoat();
        XML me = content.append("ctps");
        me.setAttribute(PATH_NAME, PATH_VALUE);
        System.out.println(content);
        //下发请求，并返回报文
        return netconfTool.get(node, content);
    }

    @Override
    public String getCtp(String node, String name) {
        return null;
    }

    @Override
    public String getFtps(String node) {
        return get(node, "ftps");
    }

    @Override
    public String getFtp(String node, String name) {
        return null;
    }
}
